﻿using CarPCBATest.Procedure;
using Common;
using ImageSourceModuleCs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using VM.Core;
using static GerberLibrary.ArtWork.Functions.FlowerThing;

namespace CarPCBATest.ViewModel
{
    public partial class HomeViewModel
    {
        public bool DC_Light(LedCurrentConfig config)
        {
            try
            {
                currentProcId = config.ProcID;
                Log = $"开始测试{config.LedDesc}功能";

                bool comRet = true;
                Thread.Sleep(500);
                // 1.4测试电流
                double current = 0;
                try
                {
                    current = DM3058E.GetCurrent() * 1000;
                }
                catch
                {
                    current = DM3058E.GetCurrent() * 1000;
                }

                Log = $"功能{config.LedDesc}电流为{current}mA";
                LocalLogs.Add(Log);
                if (current <= config.UpperLimitCurrent & current >= config.LowerLimitCurrent)
                {
                    // ok
                }
                else
                {
                    //ng
                    Log = "电流不在OK范围";
                    foreach (var item in TestResultDatas)
                    {
                        item.IsOK = false;
                        item.NgMsg.Add($"功能[{config.LedDesc}] 电流=[{current}] 不在OK范围");
                    }
                    comRet = false;
                }
                bool timeOut = false;
                vmProcHaveExec = false;
                swGlobal.Restart();
                vmResult = null;
                Application.Current.Dispatcher.Invoke(() =>
                {
                    VisionTool.SetProcVisionRet(config.ProcName, control);
                });
                // 2.视觉检测

                // sdk获取图像
                CameraTool.Open();
                var image = CameraTool.GetCurrentImage();
                //设置到图像源
                ImageSourceModuleTool imageSourceModuleTool = (ImageSourceModuleTool)VmSolution.Instance[$"{config.ProcName}.图像源1"];
                imageSourceModuleTool.SetImageData(image);

                VisionTool.RunProc(config.ProcName);
                Task.Run(() =>
                {
                    Thread.Sleep(3000);
                    timeOut = true;
                });
                while (!timeOut)
                {
                    if (vmProcHaveExec)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
                if (timeOut)
                {
                    // 超时
                    Log = "视觉判定流程执行超时";
                    foreach (var item in TestResultDatas)
                    {
                        item.IsOK = false;
                        item.NgMsg.Add($"功能[{config.LedDesc}] 视觉判定流程执行超时");
                    }
                    return false;
                }
                if (vmResult is null)
                {
                    Log = "视觉判定测试结果为null";
                    foreach (var item in TestResultDatas)
                    {
                        item.IsOK = false;
                        item.NgMsg.Add($"功能[{config.LedDesc}] 视觉判定测试结果为null");
                    }
                    return false;
                }
                Mylog.Info($"vmResult=[{vmResult}]");

                if (vmResult == null)
                {
                    comRet = false;
                    Log = "灯光视觉流程执行返回Null";
                    foreach (var item in TestResultDatas)
                    {
                        item.IsOK = false;
                        item.NgMsg.Add($"功能[{config.LedDesc}] 灯光视觉流程执行返回Null");
                    }
                    return false;
                }
                else
                {
                    var tmp = vmResult.Split(',');

                    //if (tmp.Count() != config.StationCount)
                    //{
                    //    comRet = false;
                    //    Log = "灯光视觉流程执行返回值异常";
                    //    foreach (var item in TestResultDatas)
                    //    {
                    //        item.IsOK = false;
                    //        item.NgMsg.Add($"功能[{config.LedDesc}] 灯光视觉流程执行返回值异常");
                    //    }
                    //}

                    if (tmp.Count() != 6)
                    {
                        comRet = false;
                        Log = "灯光视觉流程执行返回值异常";
                        for (int i = 0; i < config.StationCount; i++)
                        {
                            TestResultDatas[i].IsOK = false;
                            TestResultDatas[i].NgMsg.Add($"功能[{config.LedDesc}] 灯光视觉流程执行返回值异常");
                        }
                    }
                    else
                    {
                        for (int i = 0; i < config.StationCount; i++)
                        {
                            if (tmp[i] == "1")
                            {
                                Log = $"工位[{i + 1}]视觉判定OK";
                                LocalLogs.Add(Log);
                                TestResultDatas[i].IsOK = true;
                            }
                            else
                            {
                                Log = $"工位[{i + 1}]视觉判定NG";
                                LocalLogs.Add(Log);
                                TestResultDatas[i].IsOK = false;
                                TestResultDatas[i].NgMsg.Add("视觉判定NG");
                                comRet = false;
                            }
                        }
                    }
                }

                return comRet;
            }
            catch (Exception ex)
            {
                Log = ex.Message;
                Mylog.Error(ex, ex.Message);
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] {ex.Message}");
                return false;
            }
        }
    }
}